package org.ielts.service.security;

import org.ielts.domain.security.Role;
import org.ielts.repository.security.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleService {

	@Autowired
	RoleRepository roleRepository;

	@Transactional
	public Iterable<Role> listAllRole() {
		Iterable<Role> roles = roleRepository.findAll();

		return roles;
	}

	@Transactional
	public Role findByName(String name) {

		Role role = roleRepository.findOneByName(name);

		return role;
	}

	@Transactional
	public Role createRole(String name, String title) {
		Role role = new Role();
		role.setName(name);
		role.setTitle(title);

		roleRepository.save(role);

		return role;
	}

	@Transactional
	public void removeRole(String id) {
		roleRepository.delete(id);
	}

	@Transactional
	public Role modifyRole(String id, String name, String title) {
		Role role = new Role();
		role.setId(id);
		role.setName(name);
		role.setTitle(title);

		role = roleRepository.save(role);

		return role;
	}

	@Transactional
	public Role findRole(String id) {
		Role role = roleRepository.findOne(id);

		return role;
	}

}
